package com.gin.supermarket_inventory_system.commodity.dto.vo;

import com.gin.database.base.BaseVo;
import com.gin.supermarket_inventory_system.commodity.entity.CommodityPo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @since : 2023/10/9 16:41
 * @author : ginstone
 * @version : v1.0.0
 **/
@Getter
@Setter
@Schema(description = "视图对象:商品信息")
public class CommodityVo extends BaseVo implements Cloneable {
    @Schema(description = "名称(系列名称)")
    String name;

    @Schema(description = "商品类型ID")
    Long typeId;

    @Schema(description = "商品生产商ID")
    Long producerId;

    @Schema(description = "商品类型全名(填充)")
    String typeName;

    @Schema(description = "商品生产商名称(填充)")
    String producerName;


    public CommodityVo(CommodityPo po) {
        // 复制Po的字段
        BeanUtils.copyProperties(po, this);
    }

    /**
     * 将持久对象列表转换为视图对象列表
     * @param data 持久对象列表
     * @return 视图对象列表
     */
    public static List<CommodityVo> toVoList(List<CommodityPo> data) {
        if (data == null) {
            return new ArrayList<>();
        }
        return data.stream().map(CommodityVo::new).toList();
    }

    @Override
    public CommodityVo clone() {
        try {
            return (CommodityVo) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new AssertionError();
        }
    }
}
